//
//  WeiboDetailTableView.m
//  微blog
//
//  Created by Jorn.Wu on 15/8/29.
//  Copyright (c) 2015年 Jorn.Wu(jorn_wza@sina.com). All rights reserved.
//

#import "WeiboDetailTableView.h"
#import "WeiboCommentModel.h"
#import "CommentTableViewCell.h"

@interface WeiboDetailTableView ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation WeiboDetailTableView

static NSString *commentCellID = @"commentCell";

/**
 * 为控制器瘦身，将自己的数据源和代理都设为自己
 */
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        
        self.backgroundColor = [UIColor clearColor];
        self.dataSource = self;
        self.delegate = self;
        [self registerCellClass];
        
    }
    
    return self;
}

- (void)awakeFromNib{
    self.backgroundColor = [UIColor clearColor];
    self.dataSource = self;
    self.delegate = self;
    [self registerCellClass];
    
}


/**
 * 注册cell类，如果从xib中加载过来，要注册nib，在此注册类名即可
 */
- (void)registerCellClass{
    [self registerNib:[UINib nibWithNibName:@"CommentTableViewCell" bundle:nil] forCellReuseIdentifier:commentCellID];
}


/**
 * 每节为一个cell
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.weiboCommentModelAr.count;;
}


/**
 * 创建cell，将Model传过去，里面包含weibo评论信息
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:commentCellID forIndexPath:indexPath];
    WeiboCommentModel *model = self.weiboCommentModelAr[indexPath.row];
    cell.commentModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}


/**
 * 根据model计算cell的高度
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    WeiboCommentModel *model = self.weiboCommentModelAr[indexPath.row];
    return [CommentTableViewCell getCellHeightWithModel:model];
}


/**
 * 每节的节头高度
 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *titleLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
//    titleLb.backgroundColor = [UIColor cyanColor];
    titleLb.textColor = [UIColor whiteColor];
    titleLb.font = [UIFont systemFontOfSize:17];
    titleLb.text = @"评论详情：";
    return titleLb;
}


@end
